<?php

namespace app\api\logic;


use app\api\Request;
use app\common\service\UserService;

class ExamTime
{
    /**
     * getUserExamTime
     * @throws
     */
    public function getIndexExamData()
    {
        // 配置信息
        $result = [
            'show' => false,
            'exam_remain' => 0,
            'exam_date' => '', // 新增考试日期字段
        ];

        // 提取参数
        $userId = app(Request::class)->param('userId');
        if (!$userId) {
            return $result;
        }

        // 查询用户信息
        $userService = new UserService();
        $userInfo = $userService->getUserById($userId, '*');

        // 未选择专业不显示报考信息
        if (empty($userInfo['profession_id'])) {
            return $result;
        }

        // 匹配考试时间
        $examTime = app(\app\common\model\Profession::class)->getValueById($userInfo['profession_id'], 'exam_time');
        if ($examTime) {
            // 计算时间差,转换为天数    
            $timeDiff = $examTime - time();
            $result['show'] = true;
            $result['exam_remain'] = floor($timeDiff / 86400);
            $result['exam_date'] = date('Y-m-d', $examTime); // 设置考试日期
        }

        // 返回
        return $result;
    }
}